//
//  LoginViewController.m
//  预购清单
//
//  Created by  on 2022/10/17.
//

#import "LoginViewController.h"
#import "PrivateViewController.h"
@interface LoginViewController ()
@property (strong, nonatomic) IBOutlet UILabel *logo;
@property (strong, nonatomic) IBOutlet UITextField *input1;
@property (strong, nonatomic) IBOutlet UITextField *input2;
@property (strong, nonatomic) IBOutlet UITextField *input3;
@property (strong, nonatomic) IBOutlet UIButton *btn1;
@property (strong, nonatomic) IBOutlet UIButton *btn2;
@property (strong, nonatomic) IBOutlet NSLayoutConstraint *inputTop;
@property (strong, nonatomic) IBOutlet UIButton *selectBtn;
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    [self initUI];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:NO];
}
- (void)initUI{
    self.input3.hidden = self.islogin;
    self.logo.text = self.islogin ? LS(@"Welcome Back") : LS(@"Let’s Get Started");
    self.inputTop.constant = self.islogin ? -47 : 25;
    NSString *title = self.islogin ? LS(@"没有账号？注册") : LS(@"已有账号，登录");
    [self.btn1 setTitle:title forState:UIControlStateNormal];
    NSString *title2 = self.islogin ? LS(@"登录") : LS(@"注册");
    [self.btn2 setTitle:title2 forState:UIControlStateNormal];
    if (self.islogin) {
        self.btn2.enabled = self.input1.text.length > 0 && self.input2.text.length > 0;
    }else{
        self.btn2.enabled = self.input1.text.length > 0 && self.input3.text.length > 0 && self.input2.text.length > 0;
    }
}

- (IBAction)actionForBtn:(UIButton *)sender {
    if (sender.tag == 201) {
        if (self.islogin) {
            LoginViewController * vc = [LoginViewController new];
            vc.islogin = NO;
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [self backBtnAction];
        }
    }else if (sender.tag == 202){
        if ([self judgeInput]) {
            if (self.islogin) {
                [UserTool login:self.input1.text psw:self.input2.text complete:^(NSDictionary * _Nonnull dict) {
                    int code = [dict[@"code"] intValue];
                    if (code == 200) {
                        APP.user = dict[@"data"];
                        [self backBtnAction];
                    }else{
                        [CCHud msg:LS(dict[@"msg"])];
                    }
                }];
            }else{
                [UserTool register:self.input1.text account:self.input1.text psw:self.input2.text complete:^(NSDictionary * _Nonnull dict) {
                    int code = [dict[@"code"] intValue];
                    if (code == 200) {
                        LoginViewController *vc   = self.navigationController.viewControllers[self.navigationController.viewControllers.count - 2];
                        vc.input1.text = self.input1.text;
                        vc.input2.text = self.input2.text;
                        [self backBtnAction];
                    }else{
                        [CCHud msg:LS(dict[@"msg"])];
                    }
                }];
            }
        }
    }else if (sender.tag == 203){
        sender.selected = !sender.selected;
    }else if (sender.tag == 204){
        PrivateViewController *vc = [PrivateViewController new];
        [self push:vc :YES];
    }
}
- (BOOL)judgeInput{
    if(self.input1.text.length <= 0){
        [CCHud msg:LS(@"账号不能为空")];
        return NO;
    }else if (self.input2.text.length <= 0){
        [CCHud msg:LS(@"密码不能为空")];
        return NO;
    }else if (self.input3.text.length <= 0 && self.islogin == NO){
        [CCHud msg:LS(@"再次确认密码")];
        return NO;
    }else if ([self.input1.text isNumberOrAlphabet] == NO){
        [CCHud msg:LS(@"账号只能包含数字和字母")];
        return NO;
    }else if ([self.input2.text isNumberOrAlphabet] == NO){
        [CCHud msg:LS(@"密码只能包含数字和字母")];
        return NO;
    }else if ([self.input2.text isEqualToString:self.input3.text] == NO && self.islogin == NO){
        [CCHud msg:LS(@"两次密码不一致")];
        return NO;
    }else if (self.selectBtn.selected == NO){
        [CCHud msg:LS(@"请先阅读用户协议和隐私政策")];
        return NO;
    }else{
        return YES;
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return YES;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    return [string isNumberOrAlphabet] || [string isEqualToString:@""];
}

- (void)textFieldDidChangeSelection:(UITextField *)textField{
    textField.rightViewMode = textField.text.length > 0 ? UITextFieldViewModeAlways : UITextFieldViewModeNever;
    [self initUI];
}
@end
